package com.douxg.fastCoding.db;

import cn.hutool.core.util.StrUtil;
import com.douxg.fastCoding.spring.consts.BizCode;
import com.douxg.fastCoding.spring.exception.BizRuntimeException;
import com.douxg.fastCoding.web.consts.DbType;
import com.douxg.fastCoding.web.eo.DbInfoEo;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.DbKit;
import com.jfinal.plugin.activerecord.DbPro;
import com.jfinal.plugin.druid.DruidPlugin;

/**
 * @Description
 * @Auther douxg
 * @Date 2020/6/13 16:22
 */
public class DbManager {
    public static void testCon(DbInfoEo dbInfoEo) {
        String dbFlag = getDbFlag(dbInfoEo);
        if (DbKit.getConfig(dbFlag) == null) {
            synchronized (DbManager.class) {
                if (DbKit.getConfig(dbFlag) == null) {
                    connection(dbInfoEo, dbFlag, true);
                }
            }
        }
    }

    private static void connection(DbInfoEo dbInfoEo, String dbFlag, boolean isTest) {

        DruidPlugin dp = new DruidPlugin(dbInfoEo.getJdbcUrl(), dbInfoEo.getUserName(), dbInfoEo.getPassword());
        if (dbInfoEo.getDriverClass() != null) {
            dp.setDriverClass(dbInfoEo.getDriverClass());
        }
        ActiveRecordPlugin arp = new ActiveRecordPlugin(dbFlag, dp);
        DbType dbType = DbType.getByName(dbInfoEo.getDbType());
        if (dbType != null && dbType.getDialect() != null) {
            arp.setDialect(dbType.getDialect());
        }
        try {
            dp.start();
            arp.start();
            if (isTest) {
                arp.stop();
                dp.stop();
            }
        } catch (Exception e) {
            throw new BizRuntimeException().setBizCode(BizCode.DB_CON_ERROR);
        }
    }

    public static DbPro getDb(DbInfoEo dbInfoEo) {
        String dbFlag = getDbFlag(dbInfoEo);
        if (DbKit.getConfig(dbFlag) == null) {
            synchronized (DbManager.class) {
                if (DbKit.getConfig(dbFlag) == null) {
                    connection(dbInfoEo, dbFlag, false);
                }
            }
        }
        return Db.use(dbFlag);
    }

    private static String getDbFlag(DbInfoEo dbInfoEo) {
        String dbType = dbInfoEo.getDbType();
        DbType byName = DbType.getByName(dbType);
        byName.getDbFunction().handleJdbcUrl(dbInfoEo);
        return StrUtil.join(":", dbInfoEo.getHost(), dbInfoEo.getPort(), dbInfoEo.getDbName(), dbInfoEo.getUserName());
    }

    /*private static Dialect getDialect(String dbType) {
        DbType byName = DbType.getByName(dbType);
        return getDialect(byName);
    }*/

    /*private static Dialect getDialect(DbType dbType) {
        if (dbType != null) {
            if (dbType == DbType.MYSQL || dbType == DbType.MARIADB) {
                return new MysqlDialect();
            } else if (dbType == DbType.ORACLE || dbType == DbType.ORACLE_12C) {
                return new OracleDialect();
            } else if (dbType == DbType.POSTGRE_SQL) {
                return new PostgreSqlDialect();
            } else if (dbType == DbType.SQL_SERVER || dbType == DbType.SQL_SERVER2005) {
                return new SqlServerDialect();
            } else if (dbType == DbType.SQLITE) {
                return new Sqlite3Dialect();
            }
        }
        return null;
    }*/
}
